package com.api.oa.services.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.api.oa.common.UserType;
import com.api.oa.config.UserContext;
import com.api.oa.mapper.YQServiceOptMapper;
import com.api.oa.mapper.YQUserMapper;
import com.api.oa.model.entity.YQServiceOpt;
import com.api.oa.model.entity.YQUser;
import com.api.oa.model.vo.OaOrgTreeVO;
import com.api.oa.services.YQServiceOptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;


@Service
public class YQServiceOptServiceImpl implements YQServiceOptService {

    @Autowired
    private YQServiceOptMapper mapper;
    @Autowired
    private YQUserMapper yqUserMapper;
    
    


    @Override
    public Integer add(YQServiceOpt entity) {
        if(UserContext.getRole() == UserType.INS){
            entity.setInsCode(UserContext.getInsCode());
        }
        entity.setCreatedAt(DateUtil.date());
        return mapper.insert(entity);
    }

    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public Integer update(YQServiceOpt entity) {
        return mapper.update(entity);
    }

    @Override
    public PageInfo<YQServiceOpt> list(String insCode,Integer pageIndex,Integer pageSize) {
        if(UserContext.getRole() == UserType.INS){
            insCode = UserContext.getInsCode();
        }
        PageHelper.startPage(pageIndex,pageSize);
        PageHelper.orderBy("serviceId desc");
        return new PageInfo<>(mapper.list(insCode));
    }

    @Override
    public YQServiceOpt one(Long id) {
     return mapper.findOne(id);
    }

    @Override
    public List<YQServiceOpt> queryByInsCode(Long userId) {
        YQUser one = yqUserMapper.findOne(userId);

        if(StrUtil.isEmptyIfStr(one.getBelongInsCode())){
            return new ArrayList<>();
        }
        return mapper.list(one.getBelongInsCode());
    }


}
